package org.wishwork.api.inventory.upload;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.api.Api;
import org.wishwork.api.inventory.upload.request.Data;
import org.wishwork.api.inventory.upload.request.InventoryUploadRequest;
import org.wishwork.api.inventory.upload.request.Item;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 新建盘点单
 * @author: WangYX
 * @create: 2022-04-07 11:10
 * @Version: 1.0.0
 **/
public class InventoryUploadApi extends Api<InventoryUploadResponse, InventoryUploadRequest> {

    private static final Logger log = LoggerFactory.getLogger(InventoryUploadApi.class);


    private Context context;

    public InventoryUploadApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(InventoryUploadRequest inventoryUploadRequest) throws Exception {
        String url = "/open/jushuitan/inventory/upload";
        Map<String, Object> params = new HashMap<>();
        if (inventoryUploadRequest.getWmsCoId() != null) {
            params.put("wms_co_id", inventoryUploadRequest.getWmsCoId());
        }
        if (!StrUtil.isEmpty(inventoryUploadRequest.getType())) {
            params.put("type", inventoryUploadRequest.getType());
        }
        if (ObjectUtil.isNotEmpty(inventoryUploadRequest.getIsConfirm())) {
            params.put("is_confirm", inventoryUploadRequest.getIsConfirm());
        }
        if (inventoryUploadRequest.getDatas() != null && inventoryUploadRequest.getDatas().size() > 0) {
            List<Map<String, Object>> datas = new ArrayList<>();
            inventoryUploadRequest.getDatas().forEach(o -> {
                Map<String, Object> data = new HashMap<>();
                if (StrUtil.isNotEmpty(o.getSoId())) {
                    data.put("so_id", o.getSoId());
                }
                if (o.getWarehouse() != null) {
                    data.put("warehouse", o.getWarehouse());
                }
                if (StrUtil.isNotEmpty(o.getRemark())) {
                    data.put("remark", o.getRemark());
                }
                if (o.getItems() != null && o.getItems().size() > 0) {
                    List<Map<String, Object>> items = new ArrayList<>();
                    o.getItems().forEach(k -> {
                        Map<String, Object> item = new HashMap<>();
                        if (StrUtil.isNotEmpty(k.getSkuId())) {
                            item.put("sku_id", k.getSkuId());
                        }
                        if (k.getQty() != null) {
                            item.put("qty", k.getQty());
                        }
                        if (StrUtil.isNotEmpty(k.getBatchId())) {
                            item.put("batch_id", k.getBatchId());
                        }
                        if (StrUtil.isNotEmpty(k.getProducedDate())) {
                            item.put("produced_date", k.getProducedDate());
                        }
                        if (StrUtil.isNotEmpty(k.getExpirationDate())) {
                            item.put("expiration_date", k.getExpirationDate());
                        }
                        items.add(item);
                    });
                    data.put("items", items);
                }
                datas.add(data);
            });
            params.put("data", datas);
        }
        return HttpHelper.sendRequest(params, url);
    }

    @Override
    protected InventoryUploadResponse process(JSONObject data, InventoryUploadRequest inventoryUploadRequest) throws Exception {
        JSONObject datas = data.getJSONObject("datas");
        InventoryUploadResponse inventoryUploadResponse = datas.toJavaObject(InventoryUploadResponse.class);
        return inventoryUploadResponse;
    }
}

